package com.ych.DemoSSM.common.web.controller;

import org.apache.log4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class ExceptionAdvice {

    private Logger logger = Logger.getLogger(ExceptionAdvice.class);

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Map<String, Object> handleException(Exception e) throws Exception {
        if (e instanceof HttpRequestMethodNotSupportedException) {
            throw e;
        }
        logger.error("系统错误", e);

        Map<String, Object> ret = new HashMap<String, Object>();
        ret.put("code", "1");
        ret.put("message", "系统错误!");
        return ret;
    }
}
